package cn.sanshuilin.kst.system.provider.write.dao;

import cn.sanshuilin.kst.common.utils.LogPrinter;
import cn.sanshuilin.kst.common.utils.PagingData;
import cn.sanshuilin.kst.system.provider.bean.Member;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-11-27 21:03
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class MemberReq extends AbstractJpaDao<Member> {


    public Member findBy(String username){
        Query query = entityManager.createNativeQuery("SELECT * FROM Member WHERE Username ='test'", Member.class);
        return (Member) query.getSingleResult();
    }

    public List<Member> getMemberByKeyword(String strKeyword) {
      Query query = entityManager.createNativeQuery("FROM Member m WHERE m.Username LIKE :Keyword  OR m.Username LIKE :UpperCaseKeyword ORDER BY m.Username", Member.class);
        query.setParameter("Keyword","%" + strKeyword + "%");
        query.setParameter("UpperCaseKeyword", "%" +strKeyword.toUpperCase() + "%");
        return query.getResultList();
    }

    public List<Member> getMembers(int nPageIndex, int nPageSize, String strKeyword, int searchGroupIDs,
                                   int searchStatusID, int searchIsRecommendID, String searchMemberID) {

        String hql=getMembersQuery("m", strKeyword, searchGroupIDs, searchStatusID, searchIsRecommendID,
                searchMemberID,nPageIndex,nPageSize);
        Query query = entityManager.createNativeQuery("", Member.class);


        if (searchMemberID != "") {
            query.setParameter("searchMemberID", searchMemberID);
        }
        if (searchGroupIDs != -1) {
            query.setParameter("searchGroupIDs", String.valueOf(searchGroupIDs));
        }
        if (searchStatusID != -1) {
            query.setParameter("searchStatusID",String.valueOf(searchStatusID) );
        }
        //if (searchIsRecommendID != -1) {
        //	query.setInteger("searchIsRecommendID", searchIsRecommendID);
        //}
        query.setParameter("Keyword", "%" + strKeyword + "%");



        return  query.getResultList();

    }
    private String getMembersQuery(String strChange, String strKeyword, int searchGroupIDs, int searchStatusID,
                                   int searchIsRecommendID, String searchMemberID, int nPageIndex, int nPageSize) {
        String hql = "SELECT " + strChange
                + " FROM Member m WHERE (m.Username LIKE :Keyword OR m.Name LIKE :Keyword) ";

        if (searchMemberID != "") {
            hql += " AND m.ID= :searchMemberID";
        }

        if (searchGroupIDs != -1) {
            hql += " AND m.GroupID= :searchGroupIDs";
        }
        if (searchStatusID != -1) {
            hql += " AND m.StatusID= :searchStatusID";
        }
        //if (searchIsRecommendID != -1) {
        //	hql += " AND m.IsRecommendID= :searchIsRecommendID";
        //}
        hql += " ORDER BY m.GroupID ";
        hql +="LIMIT :"+nPageIndex+","+nPageSize;
        return hql;
    }

    public List<Member> getPickMembers(String strKeyword) {


            String hql = "SELECT DISTINCT m FROM Member m WHERE  (m.Username LIKE :Keyword OR m.Mobile LIKE :Keyword OR m.Name LIKE :Keyword )  ";

            hql += " AND m.GroupID = :SearchGroupID";

            hql += " ORDER BY m.Username";
            Query query = entityManager.createNativeQuery(hql, Member.class);
//            query.setParameter("SearchGroupID", String.valueOf(CDict.MemberGroupPartner.getId()));
            query.setParameter("Keyword","%" + strKeyword + "%");

        return query.getResultList();
    }
    private String getMembersQuery(String strChange, String strKeyword, int searchGroupIDs, int searchStatusID,
                                   int searchIsRecommendID, String searchMemberID) {
        String hql = "SELECT " + strChange
                + " FROM Member m WHERE (m.Username LIKE :Keyword OR m.Name LIKE :Keyword) ";

        if (searchMemberID != "") {
            hql += " AND m.ID= :searchMemberID";
        }

        if (searchGroupIDs != -1) {
            hql += " AND m.GroupID= :searchGroupIDs";
        }
        if (searchStatusID != -1) {
            hql += " AND m.StatusID= :searchStatusID";
        }
        //if (searchIsRecommendID != -1) {
        //	hql += " AND m.IsRecommendID= :searchIsRecommendID";
        //}
        hql += " ORDER BY m.GroupID ";
        return hql;
    }

    public long getMembersCount(String strKeyword, int searchGroupIDs, int searchStatusID, int searchIsRecommendID,
                                String searchMemberID) {
        long count = 0;
        try {
            String hql = this.getMembersQuery(" COUNT(*) ", strKeyword, searchGroupIDs, searchStatusID,
                    searchIsRecommendID, searchMemberID);
            Query query = entityManager.createNativeQuery(hql,Long.class);
            if (searchMemberID != "") {
                query.setParameter("searchMemberID", searchMemberID);
            }
            if (searchGroupIDs != -1) {
                query.setParameter("searchGroupIDs", String.valueOf(searchGroupIDs));
            }
            if (searchStatusID != -1) {
                query.setParameter("searchStatusID",String.valueOf(searchStatusID) );
            }
            //if (searchIsRecommendID != -1) {
            //	query.setInteger("searchIsRecommendID", searchIsRecommendID);
            //}
            query.setParameter("Keyword", "%" + strKeyword + "%");

//            Long total= memberDao.findCountByHQL(hql,params);
            Long total = (Long) query.getSingleResult();
            count=total.intValue();
        } catch (Exception e) {
            LogPrinter.error("getMembersCount" + e.getMessage());
        }
        return count;
    }


    public List<Member> getArtists(int nPageIndex, int nPageSize, int searchGroupIDs, int searchIsRecommendID,
                                   String searchSpaceID) {
        List<Member> list = new ArrayList<Member>();
        try {
            String queryHql = getArtistsQuery(" COUNT(*) ", searchGroupIDs, searchIsRecommendID, searchSpaceID);
            queryHql +="LIMIT :"+nPageIndex+","+nPageSize;
            Query query = entityManager.createNativeQuery(queryHql, Member.class);

            if (searchGroupIDs != -1) {
                query.setParameter("searchGroupIDs", String.valueOf(searchGroupIDs));
            }
            if (searchIsRecommendID != -1) {
                query.setParameter("searchIsRecommendID", String.valueOf(searchIsRecommendID));
            }
            if (searchSpaceID != null && !searchSpaceID.equals("")) {
                query.setParameter("SpaceID", String.valueOf(searchSpaceID));
            }
              list= query.getResultList();
//            list=memberDao.findListPageByHQL(queryHql,params,nPageIndex,nPageSize);
            for (Member member : list) {
                extendArtist(member);
            }
        } catch (Exception e) {
            LogPrinter.error("getMembers" + e.getMessage());
        }

        return list;
    }

    private Member extendArtist(Member member) {
        try {
            if (member != null) {
//                member.setGroupName(dictService.getDictNameByID(member.getGroupID()));
//                member.setStatusName(dictService.getDictNameByID(member.getStatusID()));
//                member.setIsRecommendName(dictService.getDictNameByID(member.getIsRecommendID()));
//                String imageID = member.getImageID();
////                String imgUrl = new AttachmentManager().getAttachmentByID(imageID).getFileNameInDisk();
//
//                String recommendProductID = member.getRecommendProductID();
//                String recommendProductImgUrl = "";
//                String recommendProductName = "";
//
//                member.setRecommendProductID(recommendProductID);
//                member.setRecommendProductImgUrl(recommendProductImgUrl);
//                member.setRecommendProductName(recommendProductName);
////                member.setIsAttention(new AttentionManager().isAttention(member.getID()));
////                member.setAttentionCount(new Long(new CountFormat().getCountFormat(new AttentionManager().getAttentionsByObjID(-1,-1,member.getID()).getCount())).intValue());
//                member.setImageUrl("");
            }

        } catch (Exception e) {
            LogPrinter.info("extendMember" + e.getMessage());
        }
        return member;
    }
    public long getArtistsCount(int searchGroupIDs, int searchIsRecommendID, String searchSpaceID) {
        long count = 0;
        try {
            String queryHql = getArtistsQuery(" COUNT(*) ", searchGroupIDs, searchIsRecommendID, searchSpaceID);
            Query query = entityManager.createNativeQuery(queryHql, Long.class);
            if (searchGroupIDs != -1) {
                query.setParameter("searchGroupIDs", String.valueOf(searchGroupIDs));
            }
            if (searchIsRecommendID != -1) {
                query.setParameter("searchIsRecommendID", String.valueOf(searchIsRecommendID));
            }
            if (searchSpaceID != null && !searchSpaceID.equals("")) {
                query.setParameter("SpaceID", String.valueOf(searchSpaceID));
            }
//            Long total= memberDao.findCountByHQL(queryHql,params);
            Long total = (Long) query.getSingleResult();
            count=total.intValue();
        } catch (Exception e) {
            LogPrinter.error("getMembersCount" + e.getMessage());
        }
        return count;
    }
    private String getArtistsByRecommendQuery(String strChange, int searchIsRecommendID) {
        String hql = "SELECT " + strChange + " FROM Member m WHERE  m.IsRecommendID = :searchIsRecommendID";

        hql += " ORDER BY m.RegistDate DESC";

        return hql;
    }
    public PagingData getArtistsByRecommend(int nPageIndex, int nPageSize, int searchIsRecommendID) {

        String listQuery=getArtistsByRecommendQuery(" m ",searchIsRecommendID);
        listQuery +="LIMIT :"+nPageIndex+","+nPageSize;
        Query query = entityManager.createNativeQuery(listQuery, Member.class);
        query.setParameter("searchIsRecommendID", String.valueOf(searchIsRecommendID));
//        List<Member> list=memberDao.findListPageByHQL(listQuery,params,nPageIndex,nPageSize);
        List<Member> list= query.getResultList();

        String countQuery=getArtistsByRecommendQuery(" COUNT(*) ",searchIsRecommendID);
        Query query1 = entityManager.createNativeQuery(countQuery);
//        Long total= memberDao.findCountByHQL(countQuery,params);
        query1.setParameter("searchIsRecommendID", String.valueOf(searchIsRecommendID));
        Long total= (Long) query1.getSingleResult();
        long count=total.intValue();
        PagingData pagingData = new PagingData();
        pagingData.setCount(count);
        pagingData.setData(list);
        return pagingData;
    }


    private String getArtistsQuery(String strChange, int searchGroupIDs, int searchIsRecommendID, String searchSpaceID) {
        String hql = "SELECT " + strChange + " FROM Member m WHERE m.StatusID = 8 ";
        if (searchGroupIDs != -1) {
            hql += " AND m.GroupID= :searchGroupIDs ";
        }
        if (searchIsRecommendID != -1) {
            hql += " AND m.IsRecommendID = :searchIsRecommendID ";
        }
        if (searchSpaceID != null && !searchSpaceID.equals("")) {
            hql += " AND m.SpaceID =:SpaceID ";
        }

        hql += " ORDER BY m.RegistDate DESC";

        return  hql;

    }
}
